package com.song.tradition.page.search

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowColumn
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.AssistChip
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilterChip
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.LineHeightStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.song.tradition.ui.theme.customBgColor
import com.song.tradition.ui.theme.customCardBgColor

@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class)
@Composable
fun SearchScreen(
    navController: NavController
) {

    val list =
        listOf("李时珍", "薄荷", "温热论", "伤寒杂病论", "本草纲目", "张仲景", "孙思邈", "补骨脂")
    val searchValue = remember {
        mutableStateOf("")
    }
    Surface(modifier = Modifier.fillMaxSize()) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(customBgColor)
        ) {
            Row(
                modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically
            ) {
                IconButton(
                    onClick = { navController.popBackStack() },
                    modifier = Modifier
                        .size(56.dp)
                        .clip(CircleShape)
                ) {
                    Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "")

                }
                Spacer(modifier = Modifier.width(8.dp))

                Card(
                    elevation = CardDefaults.cardElevation(3.dp),
                    shape = CircleShape,
                    modifier = Modifier
                        .height(36.dp)
                        .weight(1f)
                ) {

                    Row(
                        modifier = Modifier.fillMaxSize(),
                        horizontalArrangement = Arrangement.Center,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        BasicTextField(
                            value = searchValue.value,
                            onValueChange = {
                                searchValue.value = it
                            },
                            modifier = Modifier
                                .fillMaxWidth()
                                .clip(CircleShape)
                                .padding(horizontal = 8.dp, vertical = 6.dp),
                            cursorBrush = SolidColor(Color.Gray)
                        )
                    }

                }

                Spacer(modifier = Modifier.width(8.dp))

                IconButton(
                    onClick = { navController.popBackStack() }, modifier = Modifier
                        .size(56.dp)
                        .clip(CircleShape)
                ) {
                    Icon(imageVector = Icons.Default.Search, contentDescription = "")
                }


            }

            Spacer(modifier = Modifier.height(8.dp))
            Box(
                Modifier
                    .fillMaxSize()
                    .padding(horizontal = 16.dp)
            ) {
                FlowRow(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.spacedBy(10.dp),

                    ) {

                    list.forEach { item ->
                        AssistChip(onClick = {
                            searchValue.value = item
                        },
                            label = {
                                Text(text = item)
                            },
                            shape = CircleShape,

                        )

                    }
                }
            }
        }
    }
}

@Preview
@Composable
fun SearchScreenTest() {
    SearchScreen(navController = rememberNavController())
}


